﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using System.Xml;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using Microsoft.Win32;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;

namespace Srvtools
{
    [ToolboxBitmap(typeof(InfoComboBox), "Resources.InfoComboBox.ico")]
    public partial class InfoComboBox : ComboBox, ISupportInitialize, IUseSelectCommand,IWriteValue
	{
        public void BeginInit()
        {
        }

        static Hashtable ght_InfoRefVal = new Hashtable();
        public void EndInit()
        {
            if (!this.DesignMode) //Jin Add 2010-04-15 支持数据源的顺序依次为:RefVal 对象 ExternalRefVal 外部对象字符串 SelectCommand 字符串
            {
                if (this.RefVal != null)
                {
                    //优先取 RefVal
                    if (this.RefVal.DataSource != null && this.RefVal.AlwaysClose)
                    {
                        this.RefVal.AlwaysClose = false;
                        Srvtools.InfoBindingSource ibs = this.RefVal.DataSource as Srvtools.InfoBindingSource;
                        Srvtools.InfoDataSet ids = null;
                        if (ibs != null)
                        {
                            ids = ibs.DataSource as InfoDataSet;
                            ids.SetWhere("");
                        }
                        else
                        {
                            ids = this.RefVal.DataSource as Srvtools.InfoDataSet;
                            if (ids != null)
                            {
                                ids.SetWhere("");
                            }
                        }
                        this.RefVal.InnerDs = ids;
                        this.RefVal.InnerBs.DataSource = ids;
                        this.RefVal.InnerBs.DataMember = ids.RealDataSet.Tables[0].TableName;
                    }
                    else
                    {
                        if (this.RefVal.AlwaysClose)
                        {
                            this.RefVal.AlwaysClose = false;
                            this.RefVal.EndInit();
                        }                        
                    }
                    
                    //////////////////////////////////////////////////////////////
                    #region 美联新增 usas Add  by zhanghaibo 2009-02-17
                    if (this.IsUseTip == true)
                    {
                        InfoDataSet dsTmp = this.RefVal.InnerDs;
                        DataTable dt = dsTmp.RealDataSet.Tables[0];
                        DataRow dr = dt.NewRow();
                        dr[0] = DBNull.Value;
                        dr[1] = this.TipText;
                        dt.Rows.InsertAt(dr, 0);
                    }

                    #endregion
                    ////////////////////////////////////////////////////////////
                }
                else if (!string.IsNullOrEmpty(ExternalRefVal))
                {
                    //ExternalRefVal 第二
                    string[] filesNams = this.ExternalRefVal.Split('.'); //0 程序集名称(及命名空间) 1 类名 2 InfoRefVal 组件名.
                    String s = Application.StartupPath + "\\" + CliUtils.fCurrentProject + "\\";
                    string strPackage = s + filesNams[0] + ".dll";

                    string _skey = strPackage + "." + filesNams[2];
                    object orv = ght_InfoRefVal[_skey];
                    if (orv == null)
                    {
                        Assembly a = Assembly.LoadFrom(strPackage);
                        Type t = a.GetType(filesNams[0] + "." + filesNams[1]);
                        FieldInfo field = t.GetField(filesNams[2],
                            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                        if (field != null)
                        {
                            object obj = field.GetValue(Activator.CreateInstance(t));
                            if (obj is InfoRefVal)
                            {
                                this.RefVal = obj as InfoRefVal;
                                if (this.RefVal != null)
                                {
                                    if (this.RefVal.DataSource != null && this.RefVal.AlwaysClose)
                                    {
                                        this.RefVal.AlwaysClose = false;
                                        Srvtools.InfoBindingSource ibs = this.RefVal.DataSource as Srvtools.InfoBindingSource;
                                        Srvtools.InfoDataSet ids = null;
                                        if (ibs != null)
                                        {
                                            ids = ibs.DataSource as InfoDataSet;
                                            ids.SetWhere("");
                                        }
                                        else
                                        {
                                            ids = this.RefVal.DataSource as Srvtools.InfoDataSet;
                                            if (ids != null)
                                            {
                                                ids.SetWhere("");
                                            }
                                        }
                                        this.RefVal.InnerDs = ids;
                                        this.RefVal.InnerBs.DataSource = ids;
                                        this.RefVal.InnerBs.DataMember = ids.RealDataSet.Tables[0].TableName;
                                    }
                                    else
                                    {
                                        if (this.RefVal.AlwaysClose)
                                        {
                                            this.RefVal.AlwaysClose = false;
                                            this.RefVal.EndInit();
                                        }
                                    }

                                    //////////////////////////////////////////////////////////////
                                    #region 美联新增 usas Add  by zhanghaibo 2009-02-17
                                    if (this.IsUseTip == true)
                                    {
                                        InfoDataSet dsTmp = this.RefVal.InnerDs;
                                        DataTable dt = dsTmp.RealDataSet.Tables[0];
                                        DataRow dr = dt.NewRow();
                                        dr[0] = DBNull.Value;
                                        dr[1] = this.TipText;
                                        dt.Rows.InsertAt(dr, 0);
                                    }

                                    #endregion
                                    ////////////////////////////////////////////////////////////

                                    ght_InfoRefVal.Add(_skey, this.RefVal);
                                }
                            }
                        }
                    }
                    else
                    {
                        this.RefVal = orv as InfoRefVal;
                    }
                }
                if (this.RefVal != null)
                {                    
                    InnerDs = this.RefVal.InnerDs;
                    InnerBs.DataSource = InnerDs;
                    InnerBs.DataMember = InnerDs.RealDataSet.Tables[0].TableName;
                    this.DataSource = InnerBs;
                }
            }

            if (this.SelectCommand != null && this.SelectCommand != "")
            {
                //SelectCommand 第三
                if (this.SelectTop != null && this.SelectTop != "")
                {
                    StringBuilder sb = new StringBuilder(this.SelectCommand);
                    if (this.SelectCommand.IndexOf(" top ", StringComparison.OrdinalIgnoreCase) != -1)//IgnoreCase
                    {
                        string oldValue = "";
                        string[] parts = this.SelectCommand.Split(' ');
                        int i = parts.Length;
                        for (int j = 0; j < i; j++)
                        {
                            if (string.Compare(parts[j], "top", true) == 0 && j != i - 1)//IgnoreCase
                            {
                                oldValue = parts[j + 1];
                                break;
                            }
                        }
                        if (oldValue != "")
                        {
                            int lenth = this.SelectCommand.IndexOf(oldValue) + oldValue.Length;
                            sb.Replace(oldValue, this.SelectTop, 0, lenth);
                        }
                    }
                    else
                    {
                        sb.Insert(this.SelectCommand.Trim().IndexOf("select", StringComparison.OrdinalIgnoreCase) + 6, " top " + this.SelectTop);//IgnoreCase
                    }
                    this.SelectCommand = sb.ToString();
                }
                if (!this.DesignMode && this.DataSource == null) //Jin Add 2010-04-15 为免多次添加 TipText
                {
                    InnerDs.PacketRecords = -1;
                    InnerDs.RemoteName = "GLModule.cmdRefValUse";
                    InnerDs.Execute(this.SelectCommand, true);

                    //////////////////////////////////////////////////////////////
                    #region 美联新增 usas Add  by zhanghaibo 2009-02-17
                    if (this.IsUseTip == true)
                    {
                        DataTable dt = InnerDs.RealDataSet.Tables[0];
                        DataRow dr = dt.NewRow();
                        dr[0] = DBNull.Value;
                        dr[1] = this.TipText;
                        dt.Rows.InsertAt(dr, 0);
                    }

                    #endregion
                    ////////////////////////////////////////////////////////////
                    InnerBs.DataSource = InnerDs;
                    InnerBs.DataMember = "cmdRefValUse";
                    this.DataSource = InnerBs;
                }
            }
        }

        public InfoComboBox()
		{
			InitializeComponent();
            language = CliUtils.fClientLang;
            this.KeyDown += new KeyEventHandler(InfoComboBox_KeyDown);            
        }

        private bool _EnterEnable;
        [Category("Infolight"),
        Description("Indicate whether user can leave the control by pressing key of enter")]
        public bool EnterEnable
        {
            get { return _EnterEnable; }
            set { _EnterEnable = value; }
        }

        private bool _DisplayMemberOnly = false; 
        [Category("Infolight"), DefaultValue(false)]
        public bool DisplayMemberOnly
        {
            get { return _DisplayMemberOnly; }
            set { _DisplayMemberOnly = value; }
        }

        private string externalRefVal;
        [Category("Infolight"), Editor(typeof(ExternalRefvalEditor), typeof(UITypeEditor))]
        public string ExternalRefVal //Jin Add 2010-04-15 增加属性 ExternalRefVal 
        {
            get { return externalRefVal; }
            set 
            { 
                externalRefVal = value;
            }
        }

        private InfoRefVal _RefVal = null;
        [Category("Infolight")]
        public InfoRefVal RefVal  //Jin Add 2010-04-15 增加属性 RefVal 
        {
            get
            {
                return this._RefVal;
            }
            set
            {
                this._RefVal = value;                
                if (this._RefVal != null)
                {
                    this._RefVal.AlwaysClose = true;
                    if (this.DesignMode)
                    {
                        this._RefVal.EndInit();
                        object ods = this._RefVal.DataSource;
                        if (ods is InfoDataSet)
                        {
                            InnerDs = ods as InfoDataSet;
                        }
                        else if (ods is InfoBindingSource)
                        {
                            InnerDs = (ods as InfoBindingSource).DataSource as InfoDataSet;
                        }
                        if (InnerDs != null)
                        {
                            InnerBs.DataSource = InnerDs;
                            InnerBs.DataMember = InnerDs.RealDataSet.Tables[0].TableName;
                            this.DataSource = InnerBs;
                        }
                    }
                }               
            }
        }

        void InfoComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.EnterEnable && e.KeyCode == Keys.Enter)
            {
               if (base.DroppedDown ==true ) base.DroppedDown = false; // add it by mactang 2010-3-1

                if (!e.Shift)
                {
                    SendKeys.Send("{Tab}");
                }
                else
                {
                    SendKeys.Send("+{Tab}");
                }
                e.SuppressKeyPress = true;
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            BindingSourceBeginEdit();
        }

        private void BindingSourceBeginEdit()
        {
            Binding TextBinding = this.DataBindings["Text"];
            Binding SelectedValueBinding = this.DataBindings["SelectedValue"];
            if (TextBinding != null)
            {
                InfoBindingSource bindingSource = TextBinding.DataSource as InfoBindingSource;
                if (bindingSource != null)
                {
                    if (bindingSource.BeginEdit() == false)
                    {
                        TextBinding.ReadValue();
                    }
                }
            }
            if (SelectedValueBinding != null)
            {
                InfoBindingSource bindingSource = SelectedValueBinding.DataSource as InfoBindingSource;
                if (bindingSource != null)
                {
                    if (bindingSource.BeginEdit() == false)
                    {
                        SelectedValueBinding.ReadValue();
                    }
                }
            }
        }

        private SYS_LANGUAGE language;
		private DataColumn column;
        private DataTable table;
        private string oldDisplayMember = "";

        public object GetDataSource()
        {
            object obj = new object();
            if (this.SelectCommand != null && this.SelectCommand != "")
            {
                obj = this.InnerDs;
            }
            else
            {
                obj = this.DataSource;
                while (!(obj is InfoDataSet))
                {
                    if (obj is InfoBindingSource)
                    {
                        obj = ((InfoBindingSource)obj).DataSource;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            return obj;
        }

        public string GetDataMember()
        {
            string strDataMember = "";
            object obj = this.DataSource;
            if (obj is InfoDataSet && this.ValueMember.IndexOf('.') != -1)
            {
                strDataMember = this.ValueMember.Substring(0, this.ValueMember.IndexOf('.'));
            }
            else
            {
                int i = 0;
                while (strDataMember == "" && i < 5)
                {
                    if (obj is InfoBindingSource)
                    {
                        strDataMember = ((InfoBindingSource)obj).DataMember.ToString();
                        obj = ((InfoBindingSource)obj).DataSource;
                    }
                    i++;
                }
            }
            return strDataMember;
        }

        public string GetDataTable()
        {
            string strTableName = "";
            if (this.SelectCommand != null && this.SelectCommand != "")
            {
                strTableName = this.InnerDs.RemoteName;
                strTableName = strTableName.Substring(strTableName.IndexOf('.') + 1);
            }
            else
            {
                strTableName = this.GetDataMember();
                InfoDataSet infoDs = (InfoDataSet)this.GetDataSource();
                if (infoDs != null)
                {
                    if (infoDs.RealDataSet.Relations != null)
                    {
                        int i = infoDs.RealDataSet.Relations.Count;
                        for (int j = 0; j < i; j++)
                        {
                            if (infoDs.RealDataSet.Relations[j].RelationName == strTableName)
                            {
                                strTableName = infoDs.RealDataSet.Relations[j].ChildTable.TableName;
                                break;
                            }
                        }
                    }
                }
            }
            return strTableName;
        }

        private string ValueMemberBeforeDropDown;
        protected override void OnDropDown(EventArgs e)
        {
            if (this.Expression != null && this.Expression != "")
            {
                table = new DataTable();
                table.TableName = this.GetDataTable();
                column = new DataColumn();
                column.Expression = this.Expression;
                column.ColumnName = this.Expression;
                InfoDataSet ds = (InfoDataSet)this.GetDataSource();
                if (!ds.RealDataSet.Tables[table.TableName].Columns.Contains(column.ColumnName))
                {
                    ds.RealDataSet.Tables[table.TableName].Columns.Add(column);
                }
                else
                {
                    bExpColExist = true;
                }

                oldDisplayMember = this.DisplayMember;
                if (this.DataSource is InfoDataSet)
                    this.DisplayMember = table.TableName + "." + this.Expression;
                else if (this.DataSource is InfoBindingSource)
                    this.DisplayMember = this.Expression;
            }

            if (DisplayMemberOnly)
            {
                ValueMemberBeforeDropDown = this.ValueMember;
                if (DisplayMemberBeforeEnter != null && DisplayMemberBeforeEnter != String.Empty)
                    this.DisplayMember = DisplayMemberBeforeEnter;
                object selectedValue = new object();
                if (this.DataBindings["SelectedValue"] != null)
                {
                    selectedValue = this.SelectedValue;
                    this.SetFilter();
                    this.SelectedValue = selectedValue;
                    if (this.SelectedValue == null)
                    {
                        this.SelectedIndex = 0;
                    }
                }
            }

            base.OnDropDown(e);
        }

        bool bExpColExist = false;
        protected override void OnDropDownClosed(EventArgs e)
        {
            if (this.Expression != null && this.Expression != "")
            {
                object selectValue = base.SelectedValue;
                if (!bExpColExist)
                {
                    ((InfoDataSet)this.GetDataSource()).RealDataSet.Tables[table.TableName].Columns.Remove(column.ColumnName);
                }
                //2007/4/29修改，如果DisplayMember和Expression相同，無法觸發BeginEdit。
                if (this.DisplayMember == oldDisplayMember)
                {
                    this.DisplayMember = "";
                }
                this.DisplayMember = oldDisplayMember;
                if (base.SelectedValue != selectValue)
                {
                    if (selectValue != null)//can not set selectedvalue to be null
                    {
                        base.SelectedValue = selectValue;
                        BindingSourceBeginEdit();
                    }
                    else
                    {
                        SelectedIndex = -1;
                    }
                }
            }
            else if (DataSource == null)
            {
                BindingSourceBeginEdit();
            }

            if (DisplayMemberOnly)
            {
                DisplayMemberBeforeEnter = this.DisplayMember;

                object selectedValue = this.SelectedValue;
                if (this.Text == "")
                {
                    this.DisplayMember = ValueMemberBeforeDropDown;
                    this.SelectedIndex = -1;
                }
                else
                {
                    this.DisplayMember = ValueMemberBeforeDropDown;
                }

                if (this.DataBindings["SelectedValue"] != null)
                {
                    this.ClearFilter();
                    this.SelectedValue = selectedValue;
                    this.DataBindings["SelectedValue"].WriteValue();
                }
                else
                {
                    this.SelectedValue = selectedValue;
                }
            }

            if (this.RefVal != null && this.SelectedValue != null && this.DataBindings["SelectedValue"] != null) //Jin Add 2010-04-15 刷新用户的选择值
            {
                DataRow _drTmp = (((this.DataBindings["SelectedValue"] as Binding).DataSource as InfoBindingSource).CurrencyManager.Current as DataRowView).Row;
                _drTmp[(this.DataBindings["SelectedValue"] as Binding).BindingMemberInfo.BindingField] = this.SelectedValue;
                if (AllCtrls.Count > 0)
                    AllCtrls.Clear();
                GetAllCtrls(this.FindForm().Controls);
                //当存在提示项且用户选择的是提示项，则清除关联的Match值
                if (this.isUseTip && this.SelectedIndex == 0)
                {
                    foreach (ColumnMatch cm in this.RefVal.columnMatch)
                    {
                        if (!String.IsNullOrEmpty(cm.DestField) && !String.IsNullOrEmpty(cm.SrcField) && _drTmp[cm.DestField] != null)
                        {
                            float fTest = 0;
                            if (float.TryParse(_drTmp[cm.DestField].ToString(), out fTest))
                                _drTmp[cm.DestField] = 0;
                            else
                                _drTmp[cm.DestField] = "";
                            
                            //刷新绑定的其他控件值
                            foreach (Control ctrl in AllCtrls)
                            {
                                if (ctrl.DataBindings.Count > 0)
                                {
                                    Binding binding = ctrl.DataBindings[0];
                                    if (binding.DataSource == (this.DataBindings["SelectedValue"] as Binding).DataSource &&
                                        string.Compare(binding.BindingMemberInfo.BindingField, cm.DestField, true) == 0)
                                    {
                                        binding.ReadValue();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    DataRow[] _drSrcs = this.RefVal.InnerDs.RealDataSet.Tables[0].Select(this.RefVal.ValueMember + "='" + this.SelectedValue.ToString() + "'");
                    if (_drSrcs.Length > 0)
                    {

                        DataRow _drSrc = _drSrcs[0];
                        foreach (ColumnMatch cm in this.RefVal.columnMatch)
                        {
                            if (!String.IsNullOrEmpty(cm.DestField) && !String.IsNullOrEmpty(cm.SrcField))
                            {
                                if (cm.AlwaysMatch ||
                                    (_drTmp[cm.DestField] == System.DBNull.Value ||
                                    _drTmp[cm.DestField] == null || _drTmp[cm.DestField].ToString() == "" ||
                                    _drTmp[cm.DestField].ToString() == "0"))
                                {
                                    if (_drTmp.Table.Columns.IndexOf(cm.DestField) >= 0) //当列存在才更新
                                    {
                                        _drTmp[cm.DestField] = _drSrc[cm.SrcField];

                                        //刷新绑定的其他控件值
                                        foreach (Control ctrl in AllCtrls)
                                        {
                                            if (ctrl.DataBindings.Count > 0)
                                            {
                                                Binding binding = ctrl.DataBindings[0];
                                                if (binding.DataSource == (this.DataBindings["SelectedValue"] as Binding).DataSource &&
                                                    string.Compare(binding.BindingMemberInfo.BindingField, cm.DestField, true) == 0)
                                                {
                                                    binding.ReadValue();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            base.OnDropDownClosed(e);
        }

        private List<Control> AllCtrls = new List<Control>();
        private void GetAllCtrls(Control.ControlCollection ctrls) //Jin Add 2010-04-15 辅助方法
        {
            foreach (Control ctrl in ctrls)
            {
                AllCtrls.Add(ctrl);
                if (ctrl.Controls.Count > 0)
                {
                    GetAllCtrls(ctrl.Controls);
                }
            }
        }

        private string DisplayMemberBeforeEnter;
        protected override void OnEnter(EventArgs e)
        {
            if (!DesignMode)
            {
                object selectedValue = this.SelectedValue;

                DisplayMemberBeforeEnter = this.DisplayMember;
                this.DisplayMember = this.ValueMember;
                if (this.DataBindings["SelectedValue"] != null)
                {
                    this.SetFilter();
                    if (selectedValue == null) this.SelectedIndex = -1;
                    else this.SelectedValue = selectedValue;
                }
            }
            base.OnEnter(e);
        }

     
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            if (!DesignMode)
            {
                object selectedValue = this.Text.Clone();
                //this.DisplayMember = DisplayMemberBeforeEnter;
                if (this.DataBindings["SelectedValue"] != null)
                {
                    //this.ClearFilter();
                    //this.SelectedValue = selectedValue;
                    //this.DataBindings["SelectedValue"].WriteValue();

                    if (this.Text != "")//此if else判断语句，是美联新增的。  add if else by usas zhanghaibo 2009-02-17
                    {
                        selectedValue = this.Text.Clone();
                        this.ClearFilter();
                        this.SelectedValue = selectedValue;
                        this.DataBindings["SelectedValue"].WriteValue();
                    }
                    else
                    {
                        this.SelectedValue = DBNull.Value;
                        this.DataBindings["SelectedValue"].WriteValue();
                        this.Text = this.TipText;//美联
                    }
                }


            }
            //if (this.Text == "")
            //    this.SelectedIndex = -1;

            //此是美联新增的
            if (this.Text == "")
            {
                this.DisplayMember = DisplayMemberBeforeEnter;
                this.SelectedIndex = -1;
            }
            else
            {
                this.DisplayMember = DisplayMemberBeforeEnter;
            }
        }

		private string expr;

        [Category("Infolight"),
        Description("Expression of display data")]
		public string Expression
		{
			get
			{
				return expr;
			}
			set
			{
				expr = value;
			}
		}

        [Category("Infolight"),
        Description("Value of the seleced item")]
        public new object SelectedValue
        {
            get
            {
                return base.SelectedValue;
            }
            set
            {
                if (null != value)
                {
                    base.SelectedValue = value;
                }
                if (this.DataBindings["SelectedValue"] != null)
                {
                    this.DataBindings["SelectedValue"].DataSourceUpdateMode = DataSourceUpdateMode.Never;
                }
            }
        }

        private string _Filter = "";
        [Category("Infolight"),
        Description("Specifies the filter to get data")]
        public string Filter
        {
            get
            {
                return _Filter;
            }
            set
            {
                _Filter = value;
            }
        }

        public void SetFilter()
        {
            if (this.DataSource is BindingSource)
            {
                ((BindingSource)this.DataSource).Filter = this.Filter;
            }
        }

        public void ClearFilter()
        {
            /*InfoDataSet dsAllData = (InfoDataSet)this.GetDataSource();
            if (this.SelectAlias != null && this.SelectCommand != null && this.SelectAlias != "" && this.SelectCommand != "")
            {
                dsAllData.Execute(this.SelectCommand, this.SelectAlias, true);
            }
            else
            {
                dsAllData.ClearWhere();
            }*/
            if (this.DataSource is BindingSource)
            {
                ((BindingSource)this.DataSource).Filter = "";
            }
        }

        internal InfoDataSet InnerDs = new InfoDataSet();
        internal InfoBindingSource InnerBs = new InfoBindingSource();

        private string _SelectAlias;
        [Category("Infolight"),
        Description("Specifies database")]
        [Editor(typeof(ComboConnectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string SelectAlias
        {
            get
            {
                return _SelectAlias;
            }
            set
            {
                _SelectAlias = value;
            }
        }

        private string _SelectTop;
        [Category("Data")]
        public string SelectTop
        {
            get
            {
                return _SelectTop;
            }
            set
            {
                _SelectTop = value;
            }
        }

        private string _SelectCommand;
        [Category("Infolight"),
        Description("Specifies the SQL command to get data")]
        [Editor(typeof(SelectCommandEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string SelectCommand
        {
            get
            {
                return _SelectCommand;
            }
            set
            {
                _SelectCommand = value;
                if (this.Site != null && _SelectAlias != null && _SelectAlias != "" && _SelectCommand != null && _SelectCommand != "")
                {
                    InnerDs.RemoteName = "GLModule.cmdRefValUse";
                    InnerDs.Execute(_SelectCommand, _SelectAlias, true);
                    InnerBs.DataSource = InnerDs;
                    InnerBs.DataMember = "cmdRefValUse";
                    this.DataSource = InnerBs;
                }
            }
        }

        #region IWriteValue Members

        public void WriteValue()
        {
            if (this.DataBindings["SelectedValue"] != null)
            {
                this.DataBindings["SelectedValue"].WriteValue();
            }
        }

        public void WriteValue(object value)
        {
            this.SelectedValue = value;
            WriteValue();
        }

        #endregion

        #region 美联新增属性 add it by zhanghaibo 2009-02-17

        private bool isUseTip = false;
        private string tipText = "--请选择--";

        [Category("USAS")]
        public bool IsUseTip
        {
            get
            {
                return isUseTip;
            }
            set
            {
                this.isUseTip = value;
            }
        }

        [Category("USAS")]
        public string TipText
        {
            get
            {
                return tipText;
            }
            set
            {
                this.tipText = value;
            }
        }

        #endregion
    }

    public class ComboConnectionEditor : System.Drawing.Design.UITypeEditor
    {
        public ComboConnectionEditor()
        {
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        // Displays the UI for value selection.
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            IWindowsFormsEditorService EditorService = null;
            if (provider != null)
            {
                EditorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            }

            if (EditorService != null)
            {
                ListBox ColumnList = new ListBox();
                ColumnList.SelectionMode = SelectionMode.One;
                InfoComboBox combo = context.Instance as InfoComboBox;
                if (combo != null)
                {
                    XmlDocument DBXML = new XmlDocument();
                    if (File.Exists(SystemFile.DBFile))
                    {
                        DBXML.Load(SystemFile.DBFile);
                        XmlNode aNode = DBXML.DocumentElement.FirstChild;

                        while (aNode != null)
                        {
                            if (string.Compare(aNode.Name, "DATABASE", true) == 0)//IgnoreCase
                            {
                                XmlNode bNode = aNode.FirstChild;
                                while (bNode != null)
                                {
                                    ColumnList.Items.Add(bNode.LocalName);
                                    bNode = bNode.NextSibling;
                                }
                            }
                            aNode = aNode.NextSibling;
                        }
                    }
                }
                ColumnList.SelectedIndexChanged += delegate(object sender, EventArgs e)
                {
                    int index = ColumnList.SelectedIndex;
                    if (index != -1)
                    {
                        value = ColumnList.Items[index].ToString();
                    }
                    EditorService.CloseDropDown();
                };
                EditorService.DropDownControl(ColumnList);
            }
            return value;
        }
    }
}